﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AnaViewer
{
    public partial class MainForm : Form
    {   
        Properties.Settings settings = Properties.Settings.Default;


        public MainForm()
        {
            InitializeComponent();
            InitializeCommands();

            this.Text = Application.ProductName;
            this.StartPosition = FormStartPosition.CenterScreen;
            progressBarRefresh.Visible = false;
            //开始不显示，直到第一次刷新
            tslOnline.Visible = false;
        }
        void InitializeCommands()
        {
            tsbRefresh.Click += delegate(object sender, EventArgs e)
               {//SendReceive_Action() 
                   this.Cursor = Cursors.WaitCursor;
                   subject.RefreshDataAsync();
                   this.Cursor = Cursors.Default;
               };
            tsbPrevious.Click += delegate(object sender, EventArgs e) { subject.PreviousArticle(); };
            tsbChange.Click += delegate(object sender, EventArgs e) { subject.ChangeArticle(); };
            tsbNext.Click += delegate(object sender, EventArgs e) { subject.NextArticle(); };
            tsbOptions.Click += delegate(object sender, EventArgs e) { ShowOptions(); };
            tsbHelp.Click += delegate(object sender, EventArgs e) { Help_About(); };

            picYanduiLogo.Click += delegate(object sender, EventArgs e) { Yandui.GoHome(); };
        }

        AnaSubject subject;
        public AnaSubject Subject
        {
            get { return subject; }
            set
            {
                subject = value;
                if (subject == null)
                    return;


                oneAnaPanel.Subject = subject;
                anaListPanel.Subject = subject;

                subject.ItemsListChanged += delegate(object sender, EventArgs e) { UpdateStatusBar(); };

                subject.SelectedIndexChanged += delegate(object sender, EventArgs e) { UpdateStatusBar(); };
                subject.SyncStarted += delegate(object sender, EventArgs e)
                {
                    progressBarRefresh.Visible = true;
                    tslOnline.Visible = true;
                    tslOnline.Text = "正在刷新列表";
                };
                subject.SyncFinished += delegate(object sender, EventArgs e)
                {
                    progressBarRefresh.Visible = false;
                    tslOnline.Visible = true;
                    tslOnline.Text = string.Format("上次刷新时间: {0}", DateTime.Now);
                    this.Text = string.Format("{0} - {1}", Application.ProductName, subject.GetSourceName());
                };
            }
        }
        private void ShowOptions()
        {
            OptionsForm form = new OptionsForm();
            form.ShowDialog(this.TopLevelControl);
        }

        protected override void OnResize(EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
                this.Hide();

            base.OnResize(e);
        }
        //protected override void OnVisibleChanged(EventArgs e)
        //{
        //    notifyIcon.Visible = !this.Visible;
        //    base.OnVisibleChanged(e);
        //}

        void UpdateStatusBar()
        {
            tslItemCount.Text = string.Format("第 {1} 项 / 共 {0} 项", subject.AnaItems.Count, subject.SelectedIndex + 1);
        }


        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            if (isQuit)
            {//程序自己要退出
            }
            else
            {//其他原因
                switch (e.CloseReason)
                {
                    case CloseReason.UserClosing://关闭动作转为隐藏主窗体
                        e.Cancel = true;
                        this.Hide();
                        break;
                    case CloseReason.WindowsShutDown://操作系统要求，则直接退出
                        break;
                    default://询问退出
                        DialogResult result = MessageBox.Show(this.TopLevelControl, "你确定要退出“语录阅览器”吗？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (result == DialogResult.Yes)
                            e.Cancel = true;
                        break;
                }
            }
            
            if(!e.Cancel)
            {//真的要退出了
            }

            base.OnFormClosing(e);
        }
        bool isQuit = false;
        public void CloseMe()
        {
            isQuit = true;
            this.Close();
            isQuit = false;
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            subject.RefreshDataAsync();
        }
    }
}